package com.lanou3g.dllo.baidumusic.musiclibrary.ranklist;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou3g.dllo.baidumusic.R;
;

/**
 * Created by dllo on 16/6/20.
 */
public class RanklistAdapter extends BaseAdapter{

    private Context context;
    private RanklistBean ranklistBean;

    public void setRanklistBean(RanklistBean ranklistBean) {
        this.ranklistBean = ranklistBean;
        notifyDataSetChanged();
    }

    public RanklistAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return ranklistBean == null ? 0 : ranklistBean.getContent().size();
    }

    @Override
    public Object getItem(int position) {
        return ranklistBean == null ? null : ranklistBean.getContent().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_ranklist,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {

            holder = (ViewHolder) convertView.getTag();
        }

        RanklistSingleQueue.getRanklistSingleQueue(context).getImageLoader()
                .get(ranklistBean.getContent().get(position).getPic_s260(), ImageLoader.getImageListener(holder.ranklistIv,R.mipmap.ic_launcher,R.mipmap.ic_launcher));

        holder.ranklistTitleTv.setText(ranklistBean.getContent().get(position).getName());

        holder.ranklistSongName1.setText(ranklistBean.getContent().get(position).getBean().get(0).getTitle());
        holder.ranklistSingerName1.setText(ranklistBean.getContent().get(position).getBean().get(0).getAuthor());

        holder.ranklistSongName2.setText(ranklistBean.getContent().get(position).getBean().get(1).getTitle());
        holder.ranklistSingerName2.setText(ranklistBean.getContent().get(position).getBean().get(1).getAuthor());

        holder.ranklistSongName3.setText(ranklistBean.getContent().get(position).getBean().get(2).getTitle());
        holder.ranklistSingerName3.setText(ranklistBean.getContent().get(position).getBean().get(2).getAuthor());

        return convertView;
    }

    class ViewHolder{

        ImageView ranklistIv;
        TextView ranklistTitleTv,ranklistNumberOne,ranklistSongName1,ranklistSingerName1;
        TextView ranklistNumberTwo,ranklistSongName2,ranklistSingerName2;
        TextView ranklistNumberThree,ranklistSongName3,ranklistSingerName3;

        public ViewHolder(View view) {

            ranklistIv = (ImageView) view.findViewById(R.id.ranklist_iv);
            ranklistTitleTv = (TextView) view.findViewById(R.id.ranklist_title_tv);

            ranklistNumberOne = (TextView) view.findViewById(R.id.ranklist_number_one);
            ranklistSongName1 = (TextView) view.findViewById(R.id.ranklist_song_name1);
            ranklistSingerName1 = (TextView) view.findViewById(R.id.ranklist_singer_name1);

            ranklistNumberTwo = (TextView) view.findViewById(R.id.ranklist_number_two);
            ranklistSongName2 = (TextView) view.findViewById(R.id.ranklist_song_name2);
            ranklistSingerName2 = (TextView) view.findViewById(R.id.ranklist_singer_name2);

            ranklistNumberThree = (TextView) view.findViewById(R.id.ranklist_number_three);
            ranklistSongName3 = (TextView) view.findViewById(R.id.ranklist_song_name3);
            ranklistSingerName3 = (TextView) view.findViewById(R.id.ranklist_singer_name3);

        }
    }
}
